package algorithm.dp;

// 解码方法
public class LC91 {
    public int numDecodings(String ss) {
        int n = ss.length();
        char[] s = ss.toCharArray();
        // 1. 创建 dp 表
        int[] dp = new int[n+1];

        // 2. 初始化
        dp[0] = 1;

        if (s[0] != '0') dp[1] = 1;

        // 3. 填表
        for (int i = 2; i <= n; i++) {
            if (s[i-1] != '0')
                dp[i] += dp[i-1];
            if ((s[i-2]-'0')*10 + s[i-1]-'0' >= 10 && (s[i-2]-'0')*10 + s[i-1]-'0' <= 26)
                dp[i] += dp[i-2];
        }
        return dp[n];
    }
}
